package com.castudio.Service.Impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.castudio.Service.WechatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @PACKAGE_NAME: com.castudio.Service.Impl
 * @NAME: WechatServiceImpl
 * @USER: ASUS
 * @DATE: 2023/4/11
 * @TIME: 下午 04:19
 **/
@Slf4j
@Service
public class WechatServiceImpl implements WechatService {
    @Value("${wechat.appid}")
    private String appid;
    @Value("${wechat.secret}")
    private String secret;

    @Override
    public String getOpenid(String js_code) {
        RestTemplate client = new RestTemplate();
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid={appid}&secret={secret}&js_code={js_code}&grant_type=authorization_code";
        HashMap<String,Object> map = new HashMap<>();
        map.put("appid",appid);
        map.put("secret",secret);
        map.put("js_code",js_code);
        try {
            ResponseEntity response = client.getForEntity(url,String.class,map);
            String data = (String) response.getBody();
            HashMap<String,String> dataMap = JSON.parseObject(data,HashMap.class);
            String openid = dataMap.get("openid");
            return openid;
        }catch (Exception e){
            log.error(e.toString());
            return null;
        }
    }

    @Override
    public String getAccessToken() {
        RestTemplate client = new RestTemplate();
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={appid}&secret={secret}";
        HashMap<String,String> map = new HashMap<>();
        map.put("appid",appid);
        map.put("secret",secret);
        try {
            ResponseEntity response = client.getForEntity(url,HashMap.class,map);
            HashMap dataMap = (HashMap) response.getBody();
            String access_token = (String) dataMap.get("access_token");
            return access_token;
        }catch (Exception e){
            log.error(e.toString());
            return null;
        }
    }

    @Override
    public HashMap<String, String> SendWechat(String template1, String openid, JSONObject data, String page) {
        HashMap<String,String> resMap = new HashMap<>();
        RestTemplate restTemplate = new RestTemplate();
        String access_token = getAccessToken();
        String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + access_token;
        HashMap<String, Object> dataMap = new HashMap<>();
        dataMap.put("openid", openid);
        dataMap.put("template_id", template1);
        dataMap.put("data", data);
        if (page != null) {
            dataMap.put("page", page);
        }
        try {
            Map result = restTemplate.postForObject(url, dataMap, Map.class);
            log.info(result.toString());
            if (result.get("errmsg").equals("ok")) {
                resMap.put("result", "success");
            }else {
                resMap.put("result", "fail");
            }
        }catch (Exception e) {
            log.error(e.toString());
            resMap.put("result", "error");
        }
        return resMap;
    }

    @Override
    public HashMap<String, String> SendWechatR(String template2, String openid, JSONObject data, String page) {
        HashMap<String,String> resMap = new HashMap<>();
        RestTemplate restTemplate = new RestTemplate();
        String access_token = getAccessToken();
        String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + access_token;
        HashMap<String, Object> dataMap = new HashMap<>();
        dataMap.put("openid", openid);
        dataMap.put("template_id", template2);
        dataMap.put("data", data);
        if (page != null) {
            dataMap.put("page", page);
        }
        try {
            Map result = restTemplate.postForObject(url, dataMap, Map.class);
            log.info(result.toString());
            if (result.get("errmsg").equals("ok")) {
                resMap.put("result", "success");
            }else {
                resMap.put("result", "fail");
            }
        }catch (Exception e) {
            log.error(e.toString());
            resMap.put("result", "error");
        }
        return resMap;
    }

}
